package com.univ3rse.rpg;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;

public class Game implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;
	private Texture backgroundTexture;
	private Texture playerTexture;
	private Sprite backgroundSprite;
	private Sprite playerSprite;
	private boolean jumperFacingRight;
	
	@Override
	public void create() {		
		float w = Gdx.graphics.getWidth();
		float h = Gdx.graphics.getHeight();
		
		camera = new OrthographicCamera(800, 600);
		camera.position.set(400, 300, 0);
		batch = new SpriteBatch();
		
//		texture = new Texture(Gdx.files.internal("data/test.png"));
		backgroundTexture = new Texture(Gdx.files.internal("data/maze.jpg"));
		backgroundTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		playerTexture = new Texture(Gdx.files.internal("data/player.png"));
		playerTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		
		TextureRegion region = new TextureRegion(backgroundTexture, 0, 0, 1600, 600);
		
		
		backgroundSprite = new Sprite(backgroundTexture, 0, 0, 1600, 1200);
//		sprite.setSize(1.0f,1.0f);
		backgroundSprite.setOrigin(0,0);
		backgroundSprite.setPosition(0,0);
		
		playerSprite = new Sprite(playerTexture, 0, 0, 35, 60);
//		sprite.setSize(1.0f,1.0f);
		playerSprite.setOrigin(0,0);
		playerSprite.setPosition(0,0);
	}

	@Override
	public void dispose() {
		batch.dispose();
		backgroundTexture.dispose();
	}

	@Override
	public void render() {	
		float x = playerSprite.getX();
		float y = playerSprite.getY();
		if(Gdx.input.isKeyPressed(Keys.W)){
			y++;
			System.out.println("W pressed");
			
		}
		if(Gdx.input.isKeyPressed(Keys.A)){
			x--;
			System.out.println("A pressed");
			if(jumperFacingRight){
				playerSprite.flip(true, false);
				jumperFacingRight = false;
				}

		}
		if(Gdx.input.isKeyPressed(Keys.S)){
			y--;
			System.out.println("S pressed");

		}
		if(Gdx.input.isKeyPressed(Keys.D)){
			x++;
			System.out.println("D pressed");
			if(!jumperFacingRight){
			playerSprite.flip(true, false);
			jumperFacingRight = true;
			}
		}
		playerSprite.setPosition(x, y);
		camera.position.x = playerSprite.getX();
		camera.position.y = playerSprite.getY();
		camera.update();
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		batch.setProjectionMatrix(camera.combined);
		batch.begin();
		backgroundSprite.draw(batch);
		playerSprite.draw(batch);
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
